package View;

/**
 *
 * @author elach_000
 */
public class Camera {

    int positionX;
    int positionY;
    int squaresAcross;
    int squaresDown;
    int baseOffsetX;
    int baseOffsetY;
    int cameraSpeed;
    int zoomAmplitude;
    int cameraZoom;
    int windowHeight;
    int windowWidth;
    boolean showXY;
    public int MapWidth;
    public int MapHeight;

    public Camera(int positionX, int positionY) {
        this.positionX = positionX;
        this.positionY = positionY;

        this.windowHeight = 800;
        this.windowWidth = 800;

        this.MapWidth = Model.Parameter.getWidthBoard();
        this.MapHeight = Model.Parameter.getHeightBoard();

        this.squaresAcross = Math.min(MapHeight, 10);
        this.squaresDown = Math.min(MapWidth, 25);
        this.baseOffsetX = 0; //To fill all screen : (-32,-64)
        this.baseOffsetY = -32;//To see borders  : (0,-32)

        this.cameraSpeed = 20;

        this.zoomAmplitude = 8;
        this.cameraZoom = 8;

        this.adaptCamera();

    }

    public void moveByKeyboard(String direction) {

        if (direction.equals("UP")) {
            this.positionY = Tools.clamp(this.positionY - cameraSpeed, 0, (this.MapHeight - this.squaresDown) * Tile.TileStepY);
        } else if (direction.equals("DOWN")) {
            this.positionY = Tools.clamp(this.positionY + cameraSpeed, 0, (this.MapHeight - this.squaresDown) * Tile.TileStepY);
        } else if (direction.equals("LEFT")) {
            this.positionX = Tools.clamp(this.positionX - cameraSpeed, 0, (this.MapWidth - squaresAcross) * Tile.TileStepX);
        } else if (direction.equals("RIGHT")) {
            this.positionX = Tools.clamp(this.positionX + cameraSpeed, 0, (this.MapWidth - squaresAcross) * Tile.TileStepX);
        }
    }

    public void zoomPlus() {
        this.cameraZoom = Tools.clamp(this.cameraZoom * 2, 1, this.zoomAmplitude * 4);
        this.centerCamera(true);
        this.adaptCamera();
    }

    public void zoomMoins() {
        this.cameraZoom = Tools.clamp(this.cameraZoom / 2, 1, this.zoomAmplitude * 4);
        this.centerCamera(false);
        this.adaptCamera();
    }

    public void adaptCamera() {

        this.squaresAcross = Tools.clamp(windowWidth / 64 * this.zoomAmplitude / this.cameraZoom + 2, 1, MapWidth);
        this.squaresDown = Tools.clamp(windowHeight / 16 * this.zoomAmplitude / this.cameraZoom + 2, 1, MapHeight);

    }

    /* param boolean zoomPlus : true if zoom, false if dezoom*/
    private void centerCamera(boolean zoomPlus) {
        if (zoomPlus) {
            this.positionX = Tools.clamp(this.positionX + (this.squaresAcross * Tile.TileStepX / 4), 0, this.windowWidth);
            this.positionY = Tools.clamp(this.positionY + (this.squaresDown * Tile.TileStepY / 4), 0, this.windowHeight);
        } else {
            this.positionX = Tools.clamp(this.positionX - (this.squaresAcross * Tile.TileStepX / 2), 0, this.windowWidth);
            this.positionY = Tools.clamp(this.positionY - (this.squaresDown * Tile.TileStepY / 2), 0, this.windowHeight);
        }

    }

    public int getWindowHeight() {
        return windowHeight;
    }

    public void setWindowHeight(int windowHeight) {
        this.windowHeight = windowHeight;
    }

    public int getWindowWidth() {
        return windowWidth;
    }

    public void setWindowWidth(int windowWidth) {
        this.windowWidth = windowWidth;
    }
}
